package setting

import (
	"../common"
	"../web"
	"strings"
)

type SetLineObj struct {
	common.BaseHandler
}

func (self *SetLineObj) Prepare() {
	lines := *common.FetchAllRowsPtr(
		`SELECT LINE,ISNULL(DESCRIP,''),CASE LOCKED WHEN 1 THEN 'Y' ELSE 'N' END 
           FROM SFC_LINE(NOLOCK) 
          ORDER BY LINE`,
		self.Usr.Conn, new(common.LineInfo))

	self.TmplData["lines"] = lines
}

func (self *SetLineObj) ParameterCheck() bool {
	c_line := strings.TrimSpace(self.Ctx.Params["prodline"])
	c_desc := strings.TrimSpace(self.Ctx.Params["desc"])
	if c_line == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the Line.")
		return false
	}

	self.Sqlparas = append(self.Sqlparas, c_line)
	self.Sqlparas = append(self.Sqlparas, c_desc)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{strings.ToUpper(c_line)}

	return true
}

func (self *SetLineObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_SetLine ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func LineSetHandler(ctx *web.Context) {
	self := new(SetLineObj)
	self.Ctx = ctx
	self.Url = "/setline"
	self.Html = "setline.html"
	self.Rightlist = []string{"setline"}
	self.Flashstr = "Set Line: [ %v ] finished."
	self.Do(self)
}
